/**
 * 
 */
package com.example.radioclub.utilities;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.example.radioclub.data.Constants;
import com.example.radioclub.types.ChartSong;
import com.example.radioclub.types.Genre;
import com.example.radioclub.types.SearchResult;

/**
 * This class contains static methods for helping you translate or doing
 * something from JSON String to array in specified method.
 * 
 * @author wizard
 * 
 */
public class FunctionUtils {
	private final static String TAG = Constants.APP_TAG + "Function Utils";

	/**
	 * Create ArrayList instance with MusicDatatype as its data type.
	 * 
	 * @param jsonString
	 *            string in JSON format extracted exactly and directly from Http
	 *            Response.
	 * @return an instance or <b>null</b>.
	 */
	public static ArrayList<ChartSong> getMusicListFromJson(String jsonString) {
		ArrayList<ChartSong> result = new ArrayList<ChartSong>();
		JSONArray jsonArray = null;
		try {
			jsonArray = new JSONObject(jsonString).getJSONArray("dataList");
			for (int i = 0; i < jsonArray.length(); ++i) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				result.add(new ChartSong(
						jsonObject.getInt("albumId"),
						jsonObject.getInt("artistId"),
						jsonObject.getInt("songId"),
						jsonObject.getInt("playtime"),
						jsonObject.getInt("ranking"),
						jsonObject.getInt("previousRanking"),
						jsonObject.getString("albumName"),
						jsonObject.getString("artistName"),
						jsonObject.getString("songName"),
						((jsonObject.getString("modYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("vodYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("sellStreamYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("sellDrmYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("sellNonDrmYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("sellAlacarteYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("slfLyricYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("textLyricYN").contains("Y")) ? true
								: false),
						((jsonObject.getString("rbtYN") == "Y") ? true : false),
						((jsonObject.getString("availableYN").contains("Y")) ? true
								: false)));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i(TAG, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			Log.i(TAG, e.getMessage());
		}
		return result;
	}

	/**
	 * Creates array of search result based on SearchResult class.
	 * 
	 * @param jsonString
	 *            JSON string which represents search results
	 * @return an ArrayList<SearchResult> object.
	 */
	public static ArrayList<SearchResult> getSearchResultArrayFromJSON(
			String jsonString) {
		ArrayList<SearchResult> result = new ArrayList<SearchResult>();
		try {
			JSONArray jsonArray = new JSONObject(jsonString)
					.getJSONArray("dataList");
			JSONObject obj = null;
			for (int i = 0; i < jsonArray.length(); ++i) {
				obj = jsonArray.getJSONObject(i);
				result.add(new SearchResult(obj.getString("albumId"), obj
						.getString("albumName"), obj.getString("adultYN"), obj
						.getString("genreId"), obj.getString("artistId"), obj
						.getString("artistName"), obj.getString("songId"), (obj
						.getString("playtime") != "null") ? Integer
						.parseInt(obj.getString("playtime")) : 0, obj
						.getString("modYN"), obj.getString("vodYN"), obj
						.getString("sellStreamYN"), obj.getString("sellDrmYN"),
						obj.getString("sellNonDrmYN"), obj
								.getString("sellAlacarteYN"), obj
								.getString("slfLyricYN"), obj
								.getString("textLyricYN"), obj
								.getString("rtYN"), obj.getString("rbtYN"), obj
								.getString("songName"), obj
								.getString("availableYN"), (obj
								.getString("diskNo") != "null") ? Integer
								.parseInt(obj.getString("diskNo")) : 0, obj
								.getString("titleSongYN"), obj
								.getString("hitSongYN"), (obj
								.getString("drmPrice") != "null") ? Integer
								.parseInt(obj.getString("drmPrice")) : 0, (obj
								.getString("playtime") != "null") ? Integer
								.parseInt(obj.getString("nonDrmPrice")) : 0));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i(TAG, e.getMessage());
		}

		return result;
	}

	public static ArrayList<Genre> getGenreFromJSON(String jsonString) {
		ArrayList<Genre> result = new ArrayList<Genre>();
		try {
			JSONArray jsonArray = new JSONArray(jsonString);
			JSONObject obj = null;
			for (int i = 0; i < jsonArray.length(); ++i) {
				obj = jsonArray.getJSONObject(i);
				String genreId = obj.getString("genreId"), genreOrderNum = obj
						.getString("orderNum"), genreName = obj
						.getString("genreName");
				if (genreId.length() <= 4) {
					// this is main genre.
					result.add(new Genre(genreId, genreOrderNum, genreName));
				}
			}

			/*
			 * for(int k=0; k<result.size(); ++k){ Log.i(TAG,
			 * result.get(k).getGenreId()); }
			 */
			for (int j = 0; j < jsonArray.length(); ++j) {
				obj = jsonArray.getJSONObject(j);
				String genreId = obj.getString("genreId"), orderNum = obj
						.getString("orderNum"), genreName = obj
						.getString("genreName");
				if (genreId.length() > 4) {
					// this is sub genre.
					int parent = Integer.parseInt(obj.getString("orderNum"));
					Log.i(TAG, parent + " at index " + (parent - 1) + "");
					result.get(parent - 1).getGenres()
							.add(new Genre(genreId, orderNum, genreName));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static String createHtmlPage(String certificate) {
		String page = "" + "<html>" + "<head>" + "	<title>Judul</title>"
				+ "</head>" + "<body>" + "	Hello certificate = [" + certificate + "] World! <br>"
				+ "	<div id='container'></div>"
				+ "		<div id='object'>"
				+ "			<object id='sr_soundplay' width='220' height='23' type='application/x-shockwave-flash' name='sr_soundplay' data='http://melon.co.id/flash/player.swf?certificationId=" + certificate + "&contentType=M&channelCd=CH0023'>"
				+ "				<param name='allowfullscreen' value='true'> "
				+ "				<param name='allowscriptaccess' value='always'>"
				+ "				<param name='flashvars' value='streamer=rtmp://stm.melon.co.id/melon&file=melon&autostart=true&icons=false'>"
				+ "			</object>" 
				+ "		</div>" 
				+ "</body>"
				+ "</html>";
		return page;
	}

	public static String createHtmlPageNoCertificate(String songId, String channelCd) {
		String page = ""
				+ "<html>"
				+ "	<head>"
				+ "		<title>Judul</title>"
				+ "	</head>"
				+ "	<body>"
				+ "		Hello songId = [" + songId + "] World! <br>"
				+ "		<div id='container'></div>"
				+ "		<div id='object'>"
				+ "			<object id='sr_soundplay' width='220' height='23' type='application/x-shockwave-flash' name='sr_soundplay' data='http://melon.co.id/flash/player.swf?songId=" + songId + "&contentType=M&channelCd=CH0023'>"
				+ "				<param name='allowfullscreen' value='true'> "
				+ "				<param name='allowscriptaccess' value='always'>"
				+ "				<param name='flashvars' value='streamer=rtmp://stm.melon.co.id/prelisten&file=melon&autostart=true&icons=false'>"
				+ "			</object>" 
				+ "		</div>" 
				+ "	</body>" 
				+ "</html>";
		return page;
	}
}
